import axios from "axios"
import router from "../router/index"

axios.interceptors.request.use(config=>{
    if(config.url != '/api/register' && config.url != '/api/login'){
        config.headers.authorization= JSON.parse(localStorage.getItem('isLogin')).token;
    }
    return config;
})

axios.interceptors.response.use(res=>{
    if(res.data.code!=200){
        alert(res.data.msg)
    }
    if(res.data.msg==="登录已过期或访问权限受限"){
        //跳转到登录
       router.replace("/login")
    }
    return res;
})
export const reqRegister = (user) =>{
    return axios({
        url: "/api/register",
        method: "post",
        data: user,
      })
}

export const reqLogin = (user) => {
    return axios({
        url: "/api/login",
        method: "post",
        data: user
    })
}

export const reqHomecate = () => {
    return axios({
        url: "/api/getcate",
        method: "get",
    })
}


export const reqHomebanner = () => {
    return axios({
        url: "/api/getbanner",
        method: "get"
    })
}

export const reqHomegoods=()=>{
    return axios({
        url:"/api/gethortgoods",
        method:"get"
    })
}


export const reqSearch=(user)=>{
    return axios({
        url:"/api/search",
        method:"get",
        params:user
    })
}


//商品列表
export const reqList = (user) => {
    return axios({
        url: "/api/getgoodlist",
        method: "get",
        params: user,
    })
}
//商品详情
export const reqDetail = (user) => {
    return axios({
        url: "/api/getgoodsinfo",
        method: "get",
        params: user
    })
}
//全部分类信息
export const reqGetcates = ()=>{
    return axios({
        url: "/api/getcates",
        method: "get",
    })
}
//购物车添加
export const reqCartadd = (user) => {
    return axios({
        url: "/api/cartadd",
        method: "post",
        data: user,
    })
}

//购物车列表
export const reqCartlist = (user) => {
    return axios({
        url: "/api/cartlist",
        method: "get",
        params:user,
    })
}

//购物车修改
export const reqCartedit=(user)=>{
    return axios({
        url:"/api/cartedit",
        method:"post",
        data:user,
    })
}


//购物车修改
export const reqCartdelete=(user)=>{
    return axios({
        url:"/api/cartdelete",
        method:"post",
        data:user,
    })
}

//地址查询
export const reqAddresslist=(user)=>{
    return axios({
        url:"/api/addresslist",
        method:"get",
        params:user,
    })
}
//地址添加
export const reqAddressadd=(user)=>{
    return axios({
        url:"/api/addressadd",
        method:"post",
        data:user,
    })
}

//地址修改
export const reqAddressedit=(user)=>{
    return axios({
        url:"/api/addressedit",
        method:"post",
        data:user,
    })
}

//地址删除
export const reqAddressremove=(user)=>{
    return axios({
        url:"/api/addressremove",
        method:"post",
        data:user,
    })
}

//生成订单
export const reqOrderadd=(user)=>{
    return axios({
        url:"/api/orderadd",
        method:"post",
        data:user,
    })
}
